package se.sudoku;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class digitHolder extends Activity {
	//DIGITHOLDER ITEMS
	private PopupWindow digitPopup;
	private Button digit_1, digit_2, digit_3, digit_4, digit_5, digit_6, digit_7, digit_8, digit_9, digit_blank;
	private LinearLayout digitHolder;
	private Button chosenDigitBtn;
	private Activity activity;
	
	//constructor
	public digitHolder(Context c, int w) {
		activity = (Activity) c;
		
		LinearLayout.LayoutParams btnLayout = new LinearLayout.LayoutParams((int)w/6, (int)w/6);
		
		digitPopup = new PopupWindow(View.inflate(c, R.layout.digitholder, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
		digitHolder = (LinearLayout)digitPopup.getContentView().findViewById(R.id.digitholder);
        digitHolder.setOnClickListener(digitHolderListener);
        digit_1 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_1);
        digit_1.setLayoutParams(btnLayout);
        digit_1.setOnClickListener(digitHolderListener);
        digit_2 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_2);
        digit_2.setLayoutParams(btnLayout);
        digit_2.setOnClickListener(digitHolderListener);
        digit_3 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_3);
        digit_3.setLayoutParams(btnLayout);
        digit_3.setOnClickListener(digitHolderListener);
        digit_4 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_4);
        digit_4.setLayoutParams(btnLayout);
        digit_4.setOnClickListener(digitHolderListener);
        digit_5 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_5);
        digit_5.setLayoutParams(btnLayout);
        digit_5.setOnClickListener(digitHolderListener);
        digit_6 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_6);
        digit_6.setLayoutParams(btnLayout);
        digit_6.setOnClickListener(digitHolderListener);
        digit_7 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_7);
        digit_7.setLayoutParams(btnLayout);
        digit_7.setOnClickListener(digitHolderListener);
        digit_8 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_8);
        digit_8.setLayoutParams(btnLayout);
        digit_8.setOnClickListener(digitHolderListener);
        digit_9 = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_9);
        digit_9.setLayoutParams(btnLayout);
        digit_9.setOnClickListener(digitHolderListener);
        digit_blank = (Button)digitPopup.getContentView().findViewById(R.id.digitpopup_blank);
        digit_blank.setLayoutParams(btnLayout);
        digit_blank.setOnClickListener(digitHolderListener);
	}
	
	public void toggleDigitHolder(){  	
    	if(!digitPopup.isShowing()){
    		digitPopup.showAtLocation(activity.findViewById(R.id.sudokuview), Gravity.CENTER, 0, 0);
    	}
    	else{
    		digitPopup.dismiss();
    	}
    }
		
	//DIGITHOLDER LISTERNERS
	private OnClickListener digitHolderListener = new OnClickListener() {            
	    public void onClick(View v){
	    	//insert number
	    	if(v.getParent().getParent()==digitHolder){
	    		chosenDigitBtn = (Button)v;
	    		SudokuGame.setValues(Integer.parseInt(chosenDigitBtn.getText().toString()));
	    		SudokuGame.calcProgress();
	    		SudokuGame.setPlayerProgBarValue();
	    		toggleDigitHolder();
	    	}
	    	//remove inserted number
	    	else if(v==digit_blank){
	    		SudokuGame.setValues(0);
	    		SudokuGame.calcProgress();
	    		toggleDigitHolder();
	    	}
	    	//remove popup by pressing the outside
	    	else if(v==digitHolder){
	    		toggleDigitHolder();
	    	}
	    }
	};
	
}